<?php
namespace app\serveruser\traits;
use \app\manage\service\ApiBase;
/**
 * @author:      李颖
 * @DateTime:    2017-07-14 14:37:47
 * @Description: 给data添加app_id后，运行已在service中验证过权限的该条请求，仅请求一条接口
 * $isVerify 是否已经对该方法进行了验证
 */
trait AddAppIdToRunOne{


	//根据app_key获取app_id
	protected function _addAppIdToRunOne($data, $path, $validate, $logic = '', bool $isVerify = true, $scene = ''): array 
	{
		$service = new ApiBase($data['token']);

		//获取本应用信息
		$app_key = \app\manage\service\ApiBase::getAppKey();
		$appInfo = $service->oneStepRun(['app_key'=>$app_key], 'serveruser/auth/App/find', '', 1, $validate, $logic);
		if($appInfo['status'] != SUCCESS_STATUS){
    		return $appInfo;
    	}

		$data['app_id'] = $appInfo['res']['id'];
    	if($isVerify){
    		//发送在service中已验证权限的请求
	    	$sendData['query'] = json_encode([
	            ["path"=>$path,"param"=>$data]
	        ]);
	        return ApiBase::sendDataOne(config('proxy_queue_url'), $sendData);
    	}else{
    		return $service->oneStepRun($data, $path, $scene, 1, $validate, $logic);
    	}
    	
	}

	//只获取app_id
	protected function _getAppId($data, $needVerify = true, $validate, $logic): array 
	{
		$service = new ApiBase($data['token']);

		//获取本应用信息
		if($needVerify){
			return $service->oneStepRun(['app_key'=>config('yayi_admin_app_key')], 'serveruser/auth/App/find', '', 1, $validate, $logic);
		}else{
			$sendData['query'] = json_encode([
	            ["path"=>"serveruser/auth/App/find","param"=>['app_key'=>config('yayi_admin_app_key')]]
	        ]);
	        return ApiBase::sendDataOne(config('proxy_queue_url'), $sendData);
		}
		


	}


}